﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;

/// <summary>
/// Summary description for NewsDAL
/// </summary>
public class NewsDAL : BaseDAL
{
    public DataSet List(int pageIndex, int pageMax, int groupNewsId, int isActive)
    {
        DataSet ds = new DataSet();
        string[] uspParaName = new string[] { "@pageIndex", "@pageMax", "@groupNewsId", "@isActive" };
        object[] uspParaValue = new object[] { pageIndex, pageMax, groupNewsId, isActive };
        string uspName = "MiT_usp_news_list";

        ds = base.RunProcedure(uspParaName, uspParaValue, uspName);
        return ds;
    }
    public DataSet Detail(int newsId)
    {
        DataSet ds = new DataSet();

        string query = "select * from MiT_News where Id = " + newsId.ToString();
        ds = base.RunQuery(query);

        return ds;
    }
    public DataSet UpdateStatusActive(int newsid, int IsActive)
    {
        DataSet ds = new DataSet();
        string query = string.Format("Update MiT_News set IsActive={0} where id={1}; Select {0} as Id", IsActive, newsid);
        ds = base.RunQuery(query);
        return ds;
    }
    public DataSet Insert(string title, string thumbnail, string content, string description, int userid, int groupNewsId, int status, int isActive)
    {
        DataSet ds = new DataSet();
        string urlFriendly = URLHelper.URLFriendly(title);
        string query = string.Format("insert into MiT_news (title, thumbnail, content, description, CreatedTime, createdUserId, GroupNewsId, ModifiedTime, Status, IsActive, FriendlyURL)" +
            " values (N'{0}','{1}',N'{2}',N'{3}','{4}',{5},{6},'{4}',{7}, {8}, '{9}'); select * from MiT_news;",
            title, thumbnail, content, description, DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss"), userid, groupNewsId, status, isActive, urlFriendly);
        ds = base.RunQuery(query);
        return ds;
    }
    public DataSet Update(int newsid, string title, string thumbnail, string content, string description, int groupNewsId, int status, int isActive)
    {
        DataSet ds = new DataSet();
        string urlFriendly = URLHelper.URLFriendly(title);
        string query = string.Format("Update MiT_news set Title='{0}', Thumbnail='{1}', Content='{2}', Description='{3}'," + 
            " GroupNewsId={4}, ModifiedTime='{5}', Status={6}, IsActive={8}, FriendlyURL='{9}' " + 
            "WHERE Id={7}; Select {7} as Id",
            title, thumbnail, content, description, groupNewsId, DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss"), status, newsid, isActive, urlFriendly);
        ds = base.RunQuery(query);
        return ds;
    }
}